package com.tudouji.framework.task;


import com.tudouji.common.enums.*;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.project.bill.domain.Sellform;
import com.tudouji.project.bill.domain.Whoutform;
import com.tudouji.project.bill.service.ISellformService;
import com.tudouji.project.bill.service.IWhoutformService;
import com.tudouji.project.copy.domain.Copyparam;
import com.tudouji.project.copy.domain.Copytokd;
import com.tudouji.project.copy.domain.Copytokditem;
import com.tudouji.project.copy.domain.MultipartConfig;
import com.tudouji.project.copy.service.ICopyparamService;
import com.tudouji.project.copy.service.ICopytokdService;
import com.tudouji.project.copy.service.ICopytokditemService;
import com.tudouji.project.copy.service.IMultipartConfigService;
import com.tudouji.project.customer.domain.Customer;
import com.tudouji.project.customer.service.ICustomerService;
import com.tudouji.project.organization.domain.Department;
import com.tudouji.project.organization.domain.Organization;
import com.tudouji.project.organization.domain.Supplier;
import com.tudouji.project.organization.service.IDepartmentService;
import com.tudouji.project.organization.service.IOrganizationService;
import com.tudouji.project.organization.service.ISupplierService;
import com.tudouji.project.product.domain.Innerprice;
import com.tudouji.project.product.domain.Productinfo;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.service.IInnerpriceService;
import com.tudouji.project.product.service.IProductinfoService;
import com.tudouji.project.product.service.ISalesspecificationService;
import com.tudouji.project.store.domain.Stockunit;
import com.tudouji.project.store.domain.Storehouseroom;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStockunitService;
import com.tudouji.project.store.service.IStorehouseroomService;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.Dict;
import com.tudouji.project.system.domain.Employee;
import com.tudouji.project.system.domain.SysConfig;
import com.tudouji.project.system.service.IDictService;
import com.tudouji.project.system.service.IEmployeeService;
import com.tudouji.project.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据加工task
 */
@Slf4j
@Component("processTask")
public class ProcessTask {

    @Autowired
    private ICopytokdService copytokdService;

    @Autowired
    private ICopytokditemService copytokditemService;

    @Autowired
    private IOrganizationService organizationService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IStoreroomService storeroomService;

    @Autowired
    private IStorehouseroomService storehouseroomService;

    @Autowired
    private IProductinfoService productinfoService;

    @Autowired
    private ISalesspecificationService salesspecificationService;

    @Autowired
    private IInnerpriceService innerpriceService;

    @Autowired
    private ICopyparamService copyparamService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDictService dictService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IStockunitService stockunitService;

    @Autowired
    private ISellformService sellformService;

    @Autowired
    private IMultipartConfigService multipartConfigService;


    @Autowired
    private IWhoutformService whoutformService;

    @Autowired
    private DataExtraction dataExtraction;

    private List<Dict> dictList = new ArrayList<Dict>();

    private List<Customer> customerList = new ArrayList<Customer>();

    private List<Supplier> supplierList = new ArrayList<Supplier>();

    private List<Storehouseroom> storehouseroomList = new ArrayList<Storehouseroom>();

    private List<Storeroom> storeroomList = new ArrayList<Storeroom>();

    private List<Organization> organizationList = new ArrayList<Organization>();

    private List<Productinfo> productinfoList = new ArrayList<Productinfo>();

    private List<Salesspecification> salesspecificationList = new ArrayList<Salesspecification>();

    private List<Copyparam> copyparamList = new ArrayList<Copyparam>();

    private List<Innerprice> innerpriceList = new ArrayList<Innerprice>();

    private List<Employee> employeeList = new ArrayList<Employee>();

    private List<Department> departmentList = new ArrayList<Department>();

    private List<Stockunit> stockunitList = new ArrayList<Stockunit>();

    private BigDecimal innDiscount;

    private ThreadLocal<Set<Long>> parentIds = new ThreadLocal<>();

    @Autowired
    private InnerPriceProcess innerPriceProcess;

    public void initInnDiscount() {
        String dis = this.sysConfigService.selectConfigByKey("inner_discount");
        if (StringUtils.isNotEmpty(dis)) {
            innDiscount = new BigDecimal(dis);
        }
    }

    public void init() {
        customerList = this.customerService.selectCustomerList(new Customer());
        supplierList = this.supplierService.selectSupplierList(new Supplier());
        storehouseroomList = this.storehouseroomService.selectStorehouseroomList(new Storehouseroom());
        storeroomList = this.storeroomService.selectStoreroomList(new Storeroom());
        organizationList = this.organizationService.selectOrganizationList(new Organization());
        productinfoList = this.productinfoService.selectProductinfoList(new Productinfo());
        salesspecificationList = this.salesspecificationService.selectSalesspecificationList(new Salesspecification());
        //copyparam = this.copyparamService.selectCopyparamByType(CopyTypeEnums.SALE.getCode());
        copyparamList = this.copyparamService.selectCopyparamList(new Copyparam());
        innerpriceList = this.innerpriceService.selectInnerpriceList(new Innerprice());
        dictList = this.dictService.selectDictByType(92L);
        employeeList = this.employeeService.selectEmployeeList(new Employee());
        departmentList = this.departmentService.selectDepartmentList(new Department());
        stockunitList = this.stockunitService.selectStockunitList(new Stockunit());
        innerPriceProcess.init(salesspecificationList,stockunitList,customerList,supplierList);
    }

    public Copyparam getCopyParam(Copytokd copytokd) {
        List<Copyparam> copyparams = copyparamList.stream().filter(c -> copytokd.getCode().substring(0, 2).equals(c.getBusinessPrefix())).collect(Collectors.toList());
        if (copyparams.size() == 1) {
            return copyparams.get(0);
        } else {
            return null;
        }
    }

    /**
     * 数据加工
     */
    public void process() {
        try {
            Date date = DateUtils.getNowDate();
            log.info("==============数据加工开始处理===================");
            String proess = sysConfigService.selectConfigByKey("job_process");
            if ("1".equals(proess) || "3".equals(proess) || "5".equals(proess)) {
                return;
            }

            SysConfig sysConfig = this.sysConfigService.selectConfigById(4L);
            sysConfig.setConfigValue("3");
            this.sysConfigService.updateConfig(sysConfig);

            init();

            initInnDiscount();

            List<Integer> statusList = new ArrayList<Integer>();
            statusList.add(CopyStatusEnums.INIT.getCode());
            statusList.add(CopyStatusEnums.CANT.getCode());
            statusList.add(CopyStatusEnums.FAIL.getCode());
            statusList.add(CopyStatusEnums.SPLIT.getCode());
            statusList.add(CopyStatusEnums.NO.getCode());
            Copytokd copytokd = new Copytokd();
            copytokd.setStatusList(statusList);
            List<Copytokd> copytokdList = this.copytokdService.selectcopytokdAllList(copytokd);
            for (Copytokd c : copytokdList) {
                if (c.getStatus().intValue() == CopyStatusEnums.SPLIT.getCode().intValue()){
                    continue;
                }
                Copyparam copyparam = getCopyParam(c);
                c.setCopyRemark("");
                BigDecimal total = BigDecimal.ZERO;

                //库房
                if (StringUtils.isNotEmpty(c.getSrguid())) {
                    List<Storeroom> storerooms = storeroomList.stream().filter(s -> c.getSrguid().equals(s.getGuid())).collect(Collectors.toList());
                    if (storerooms.size() == 1) {
                        c.setSrname(storerooms.get(0).getName());
                    } else if (storerooms.size() == 0) {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + "没有找到库房数据,");
                    } else {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + "库房找到多条数据,");
                    }
                } else {
                    //检查当前单据如果是拆分数据，调用新增加的获取销售中转库方法将返回的库房id设置到待同步数据中
                    //是否拆分 1是  0否

                    if (c.getIssplit() == 1) {
                        Storeroom room = getStoreRoomIdByOrgId(c.getOrgguid());
                        if (room != null) {
                            c.setSrguid(room.getGuid());
                            c.setSrname(room.getName());
                        } else {
                            c.setCopyRemark(c.getCopyRemark() + "没有库房id,");
                            if (!getSplit(c.getStatus())) {
                                c.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }
                    } else {
                        c.setCopyRemark(c.getCopyRemark() + "没有库房id,");
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                    }
                }

                //库存组织
                if (StringUtils.isNotEmpty(c.getOrgguid())) {
                    List<Organization> organizations = organizationList.stream().filter(o -> c.getOrgguid().equals(o.getGuid())).collect(Collectors.toList());
                    if (organizations.size() == 1) {
                        c.setOrgname(organizations.get(0).getName());
                        if (StringUtils.isNotEmpty(organizations.get(0).getKingdeenum())) {
                            c.setOrgKdn(organizations.get(0).getKingdeenum());
                        } else {
                            if (!getSplit(c.getStatus())) {
                                c.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                            c.setCopyRemark(c.getCopyRemark() + ",库存组织没有金蝶编号");
                        }
                    } else if (organizations.size() == 0) {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + ",没有找到组织数据");
                    } else {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + ",库存组织找到多条数据");
                    }
                } else {
                    c.setCopyRemark(c.getCopyRemark() + "没有库存组织id,");
                    if (!getSplit(c.getStatus())) {
                        c.setStatus(CopyStatusEnums.CANT.getCode());
                    }
                }


                //处理 kdtype =006 及supplierid为空的 数据 bug修复
                Whoutform out = null;
                //查询供应商
                Supplier supplier = null;
                if (c.getKdtype().equals("006") && c.getSupplierid() == null) {
                    out = whoutformService.selectWhoutformById(c.getLinkedautoid());
                    //查询供应商
                    supplier = supplierService.selectSupplierByOrgId(out.getOrgguid());
                    c.setSupplierid(supplier.getId());
                    c.setSuppliername(supplier.getName());
                    c.setSupplierKdn(supplier.getKingdeenum());
                    copytokdService.updateCopytokd(c);
                }


                //供应商
                if (c.getSupplierid() != null) {
                    List<Supplier> suppliers = supplierList.stream().filter(s -> c.getSupplierid().equals(s.getId())).collect(Collectors.toList());
                    if (suppliers.size() == 1) {
                        c.setSuppliername(suppliers.get(0).getName());
                        if (StringUtils.isNotEmpty(suppliers.get(0).getKingdeenum())) {
                            c.setSupplierKdn(suppliers.get(0).getKingdeenum());
                        } else {
                            if (!getSplit(c.getStatus())) {
                                c.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                            c.setCopyRemark(c.getCopyRemark() + ",供应商没有金蝶编号");
                        }
                    } else if (suppliers.size() == 0) {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + ",没有找到供应商数据");
                    } else {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + ",供应商找到多条数据");
                    }
                }

                //客户
                if (StringUtils.isNotEmpty(c.getCustomerid())) {
                    //优化6：判断当前待同步数据如果是其他出库单（kdtype=009），则不需要补全客户的金蝶编码。
//                    if(!c.getKdtype().equals(KDWhoutformTypeEnums.OTHER.getCode())){
                    List<Customer> customers = customerList.stream().filter(cu -> c.getCustomerid().equals(cu.getGuid())).collect(Collectors.toList());
                    if (customers.size() == 1) {
                        c.setCustomername(customers.get(0).getName());
                        if (StringUtils.isNotEmpty(customers.get(0).getKingdeenum())) {
                            c.setCustomerKdn(customers.get(0).getKingdeenum());
                        } else {
                            if (!getSplit(c.getStatus())) {
                                c.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                            c.setCopyRemark(c.getCopyRemark() + ",客户没有金蝶编号");
                        }
                    } else if (customers.size() == 0) {
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                        c.setCopyRemark(c.getCopyRemark() + ",没有找到客户数据");
                    } else {
                        c.setStatus(CopyStatusEnums.CANT.getCode());
                        c.setCopyRemark(c.getCopyRemark() + ",客户找到多条数据");
                    }
//                    }
                }

                //币种
                if (c.getCurrency() != null) {
                    List<Dict> dicts = dictList.stream().filter(d -> c.getCurrency().equals(d.getDictkey())).collect(Collectors.toList());
                    if (dicts.size() == 1) {
                        if (StringUtils.isNotEmpty(dicts.get(0).getRemark())) {
                            c.setCurrencyKdn(dicts.get(0).getRemark());
                        } else {
                            c.setCopyRemark(c.getCopyRemark() + "没有找到币种金蝶编号,");
                            if (!getSplit(c.getStatus())) {
                                c.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }
                    } else if (dicts.size() == 0) {
                        c.setCopyRemark(c.getCopyRemark() + "没有找到币种,");
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                    } else {
                        c.setCopyRemark(c.getCopyRemark() + "找到多条收款币种,");
                        if (!getSplit(c.getStatus())) {
                            c.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                    }
                }
//            else {
//                c.setCopyRemark(c.getCopyRemark() + "没有币种数据,");
//                c.setStatus(CopyStatusEnums.CANT.getCode());
//            }

                //人员
                if (StringUtils.isNotEmpty(c.getOperator())) {
                    List<Employee> employees = employeeList.stream().filter(e -> c.getOperator().equals(e.getUserid())).collect(Collectors.toList());
                    if (employees.size() == 1) {
                        c.setOperatorKdn(employees.get(0).getOwnerKdn());
                    }
                }

                //部门
                if (StringUtils.isNotEmpty(c.getDepartmentid())) {
                    List<Department> departments = departmentList.stream().filter(d -> c.getDepartmentid().equals(d.getGuid())).collect(Collectors.toList());
                    if (departments.size() == 1) {
                        c.setDepartmentname(departments.get(0).getName());
                        c.setDepartmentkdnum(departments.get(0).getKingdeenum());
                    }
                }

                boolean flag = true;
                //查询明细数据
                Copytokditem copytokditem = new Copytokditem();
                copytokditem.setCtkdid(c.getId());
                List<Copytokditem> items = copytokditemService.selectCopytokditemList(copytokditem);
                if (items != null) {
                    //明细
                    for (Copytokditem item : items) {
                        item.setCopyRemark("");
                        item.setStatus(CopyStatusEnums.WAIT.getCode());

                        //库存调拨  调入库房处理·
                        if (StringUtils.isNotEmpty(item.getToSRGuid())) {
                            List<Storeroom> storerooms = storeroomList.stream().filter(s -> item.getToSRGuid().equals(s.getGuid())).collect(Collectors.toList());
                            if (storerooms.size() == 1) {
                                item.setToSRName(storerooms.get(0).getName());
                            } else if (storerooms.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到入库库房数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条入库库房信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //库房
                        if (StringUtils.isNotEmpty(item.getSrguid())) {
                            List<Storeroom> storerooms = storeroomList.stream().filter(s -> item.getSrguid().equals(s.getGuid())).collect(Collectors.toList());
                            if (storerooms.size() == 1) {
                                item.setSrname(storerooms.get(0).getName());
                            } else if (storerooms.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到库房数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条库房信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        } else {
                            //拆分订单并且明细库房id为空、主表库房id不空的情况下，明细库房id设置为主表库房id。
                            if (c.getIssplit() == 1 && StringUtils.isNotBlank(c.getSrguid())) {
                                item.setSrname(c.getSrname());
                                item.setSrguid(c.getSrguid());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有库房id,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }
                        //库存调拨库间处理
                        if (StringUtils.isNotEmpty(item.getToSHRguid())) {
                            List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> item.getToSHRguid().equals(s.getGuid())).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                item.setToSHRName(storehouserooms.get(0).getName());
                                if (StringUtils.isNotEmpty(storehouserooms.get(0).getKingdeenum())) {
                                    item.setToSHRKdn(storehouserooms.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到入库库间金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else if (storehouserooms.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到入库库间数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条入库库间信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }
                        //库间
                        if (StringUtils.isNotEmpty(item.getShrguid())) {
                            List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> item.getShrguid().equals(s.getGuid())).collect(Collectors.toList());
                            if (storehouserooms.size() == 1) {
                                item.setShrname(storehouserooms.get(0).getName());
                                if (StringUtils.isNotEmpty(storehouserooms.get(0).getKingdeenum())) {
                                    item.setShrKdn(storehouserooms.get(0).getKingdeenum());
                                    item.setSrKdn(storehouserooms.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到库间金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else if (storehouserooms.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到库间数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条库间信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        } else {
                            //拆分订单并且库间id为空的情况下，利用当前物料类型（原料、次品、成品、辅料）、库房id调用新增的获取库间的方法，不全库间id。
                            if (c.getIssplit() == 1) {
                                Salesspecification spe = getSpe(item.getSpguid());
                                if (spe != null) {
                                    Storehouseroom storehouseroom = getStoreHouseIdBySrId(c.getSrguid(), spe.getType());
                                    if (storehouseroom != null) {
                                        item.setShrguid(storehouseroom.getGuid());
                                        item.setShrname(storehouseroom.getName());
                                        if (StringUtils.isNotEmpty(storehouseroom.getKingdeenum())) {
                                            item.setShrKdn(storehouseroom.getKingdeenum());
                                            item.setSrKdn(storehouseroom.getKingdeenum());
                                        }
                                    } else {
                                        flag = false;
                                        item.setCopyRemark(item.getCopyRemark() + "没有库间id,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有库间id,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有库间id,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //物料
                        if (StringUtils.isNotEmpty(item.getSpguid())) {
                            List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s -> item.getSpguid().equals(s.getGuid())).collect(Collectors.toList());
                            if (salesspecifications.size() == 1) {
                                item.setSpname(salesspecifications.get(0).getName());
                                //item.setKdqty(salesspecifications.get(0).getConver().multiply(item.getQty()));
                                getKdQty(item, null, salesspecifications.get(0));
                                if (StringUtils.isNotEmpty(salesspecifications.get(0).getKingdeenum())) {
                                    item.setMaterialKdn(salesspecifications.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到物料金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                                if (StringUtils.isNotEmpty(salesspecifications.get(0).getKingdeeunit())) {
                                    //20231218 金蝶单位设置错误
                                    //Stockunit unit = stockunitService.selectStockunitById(salesspecifications.get(0).getStockunitid());
                                    Stockunit unit = stockunitService.selectStockunitById(salesspecifications.get(0).getKingdeeunitid());
                                    item.setKingdeeunit(unit.getKdunit());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到物料金蝶单位,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                                //单价
                                //20230324 可以增加条件||c.getCode().startsWith("SF")，
                                // 处理在数据拉取阶段参数设置错误、之后修改参数重新通过数据加工改正错误数据的问题
                                if (item.getPrice() == null) {
                                    //20230306 只处理单价为空的，跑一段时间，copytokditem能记录对应的出库单明细id的时候，
                                    //不考虑性能问题，销售订单的单价就可以统一在数据加工阶段处理，数据拉取可以不处理
                                    if(!innerPriceProcess.setPriceAndAmount(c,item,null,null,null)){
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                        flag = false;
                                    }
                                    /**
                                    if (copyparam != null) {
                                        BigDecimal price = getInnprice(c, salesspecifications.get(0));
                                        if (copyparam.isInnerprice()) {
                                            if (price.compareTo(new BigDecimal(-1)) == 0) {
                                                flag = false;
                                                item.setCopyRemark(item.getCopyRemark() + "没有找到单价,");
                                                item.setStatus(CopyStatusEnums.CANT.getCode());
                                            } else {
                                                item.setPrice(price);
                                            }
                                        } else {
                                            if (price.compareTo(new BigDecimal(-1)) == 0) {
                                                item.setPrice(BigDecimal.ZERO);
                                            } else {
                                                item.setPrice(price);
                                            }
                                        }
                                    } else {
                                        item.setCopyRemark(item.getCopyRemark() + "没有找到同步参数,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                     **/

                                }
                                //加税合计
                                if (item.getAmount() == null) {
                                    if (item.getPrice() != null && item.getKdqty() != null) {
                                        item.setAmount(item.getPrice().multiply(item.getKdqty()));
                                        total = total.add(item.getAmount());
                                    } else {
                                        item.setCopyRemark(item.getCopyRemark() + "没有价格或者总价,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                }


                            } else if (salesspecifications.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到物料数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条物料信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        } else {
                            flag = false;
                            item.setCopyRemark(item.getCopyRemark() + "没有物料id,");
                            item.setStatus(CopyStatusEnums.CANT.getCode());
                        }

                        //产品
                        if (StringUtils.isNotEmpty(item.getProductguid())) {
                            List<Productinfo> productinfos = productinfoList.stream().filter(p -> item.getProductguid().equals(p.getGuid())).collect(Collectors.toList());
                            if (productinfos.size() == 1) {
                                item.setProductname(productinfos.get(0).getProductname());

                                item.setTaxrate(productinfos.get(0).getTax());

                                if (item.getAmount() != null && item.getPrice() != null) {

                                    item.setNotaxprice(item.getPrice().divide(new BigDecimal(1).add(productinfos.get(0).getTax().divide(new BigDecimal(100), 2)), 6));

                                    item.setNotaxamount(item.getAmount().divide(new BigDecimal(1).add(productinfos.get(0).getTax().divide(new BigDecimal(100), 2)), 2));

//                                    item.setTax(item.getNotaxamount().multiply(productinfos.get(0).getTax().divide(new BigDecimal(100),2)));
                                    item.setTax(item.getAmount().subtract(item.getNotaxamount()));
                                }


                            } else if (productinfos.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到产品信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条产品信息,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //库存组织
                        if (StringUtils.isNotEmpty(item.getOrgguid())) {
                            List<Organization> organizations = organizationList.stream().filter(o -> item.getOrgguid().equals(o.getGuid())).collect(Collectors.toList());
                            if (organizations.size() == 1) {
                                item.setOrgname(organizations.get(0).getName());
                                if (StringUtils.isNotEmpty(organizations.get(0).getKingdeenum())) {
                                    item.setOrgKdn(organizations.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到库存组织金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else if (organizations.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到库存组织,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条库存组织,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        } else {
                            flag = false;
                            item.setCopyRemark(item.getCopyRemark() + "没有库存组织id,");
                            item.setStatus(CopyStatusEnums.CANT.getCode());
                        }

                        //销售组织
                        if (StringUtils.isNotEmpty(item.getSaleorgid())) {
                            List<Organization> organizations = organizationList.stream().filter(o -> item.getSaleorgid().equals(o.getGuid())).collect(Collectors.toList());
                            if (organizations.size() == 1) {
                                if (StringUtils.isNotEmpty(organizations.get(0).getKingdeenum())) {
                                    item.setSaleorgKdn(organizations.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到销售组织金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else if (organizations.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到销售组织,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条销售组织,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //处理 kdtype =006 及supplierid为空的 数据 bug修复
                        if (c.getKdtype().equals("006") && item.getSupplierid() == null) {
                            item.setSupplierid(supplier.getId());
                            item.setBalancesupplierid(supplier.getId());
                            item.setBalancesupplierKdn(supplier.getKingdeenum());

                            item.setSuppliername(supplier.getName());
                            item.setSupplierKdn(supplier.getKingdeenum());
                            copytokditemService.updateCopytokditem(item);
                        }

                        //结算供应商
                        if (item.getBalancesupplierid() != null) {
                            List<Supplier> suppliers = supplierList.stream().filter(s -> item.getBalancesupplierid().equals(s.getId())).collect(Collectors.toList());
                            if (suppliers.size() == 1) {
                                item.setSuppliername(suppliers.get(0).getName());
                                if (StringUtils.isNotEmpty(suppliers.get(0).getKingdeenum())) {
                                    item.setBalancesupplierKdn(suppliers.get(0).getKingdeenum());
                                    item.setSupplierKdn(suppliers.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到供应商金蝶编号,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            } else if (suppliers.size() == 0) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到供应商,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            } else {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "找到多条供应商数据,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //应收客户
                        if (!c.getKdtype().equals(KDWhoutformTypeEnums.OTHER.getCode())) {
                            if (StringUtils.isNotEmpty(item.getBalancecustomerid())) {
                                List<Customer> customers = customerList.stream().filter(cu -> item.getBalancecustomerid().equals(cu.getGuid())).collect(Collectors.toList());
                                if (customers.size() == 1) {
                                    if (StringUtils.isNotEmpty(customers.get(0).getKingdeenum())) {
                                        item.setBalancecustomerKdn(customers.get(0).getKingdeenum());
                                    } else {
                                        flag = false;
                                        item.setCopyRemark(item.getCopyRemark() + "没有找到应收客户金蝶编号,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                } else if (customers.size() == 0) {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到应收客户,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "找到多条应收客户数据,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            }

                            //订货客户
                            if (StringUtils.isNotEmpty(item.getOrdercustomerid())) {
                                List<Customer> customers = customerList.stream().filter(cu -> item.getOrdercustomerid().equals(cu.getGuid())).collect(Collectors.toList());
                                if (customers.size() == 1) {
                                    if (StringUtils.isNotEmpty(customers.get(0).getKingdeenum())) {
                                        item.setOrdercustomerKdn(customers.get(0).getKingdeenum());
                                    } else {
                                        flag = false;
                                        item.setCopyRemark(item.getCopyRemark() + "没有找到订货客户金蝶编号,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                } else if (customers.size() == 0) {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到订货客户,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "找到多条订货客户数据,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            }

                            //收款客户
                            if (StringUtils.isNotEmpty(item.getPaymentcustomerid())) {
                                List<Customer> customers = customerList.stream().filter(cu -> item.getPaymentcustomerid().equals(cu.getGuid())).collect(Collectors.toList());
                                if (customers.size() == 1) {
                                    if (StringUtils.isNotEmpty(customers.get(0).getKingdeenum())) {
                                        item.setPaymentcustomerKdn(customers.get(0).getKingdeenum());
                                    } else {
                                        flag = false;
                                        item.setCopyRemark(item.getCopyRemark() + "没有找到收款客户金蝶编号,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                } else if (customers.size() == 0) {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到收款客户,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "找到多条收款客户数据,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }
                            }
                        }

                        //人员
                        if (StringUtils.isNotEmpty(item.getOwner())) {
                            List<Employee> employees = employeeList.stream().filter(e -> item.getOwner().equals(e.getUserid())).collect(Collectors.toList());
                            if (employees.size() == 1) {
                                item.setOwnerKdn(employees.get(0).getOwnerKdn());
                            }
                        }

                        //部门
                        if (StringUtils.isNotEmpty(item.getDepartmentid())) {
                            List<Department> departments = departmentList.stream().filter(d -> c.getDepartmentid().equals(d.getGuid())).collect(Collectors.toList());
                            if (departments.size() == 1) {
                                item.setDepartmentname(departments.get(0).getName());
                                if (StringUtils.isNotEmpty(departments.get(0).getKingdeenum())) {
                                    item.setDepartmentkdnum(departments.get(0).getKingdeenum());
                                } else {
                                    flag = false;
                                    item.setCopyRemark(item.getCopyRemark() + "没有找到成本中心金蝶编码,");
                                    item.setStatus(CopyStatusEnums.CANT.getCode());
                                }

                            }
                        } else {
                            if (c.getKdtype().equals(KDWhoutformTypeEnums.PRODUCTION.getCode())) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到成本中心,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        //成本对象
                        if (StringUtils.isNotEmpty(item.getCosguid())) {
                            List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s -> s.getGuid().equals(item.getCosguid())).collect(Collectors.toList());
                            if (salesspecifications.size() == 1) {
                                item.setCosname(salesspecifications.get(0).getName());
                                if (c.getKdtype().equals(KDWhoutformTypeEnums.PRODUCTION.getCode())) {
                                    if (StringUtils.isNotEmpty(salesspecifications.get(0).getKingdeenum())) {
                                        item.setCoskdnum(salesspecifications.get(0).getKingdeenum());
                                    } else {
                                        flag = false;
                                        item.setCopyRemark(item.getCopyRemark() + "没有找到成本对象金蝶编号,");
                                        item.setStatus(CopyStatusEnums.CANT.getCode());
                                    }
                                }
                            }
                        } else {
                            if (c.getKdtype().equals(KDWhoutformTypeEnums.PRODUCTION.getCode())) {
                                flag = false;
                                item.setCopyRemark(item.getCopyRemark() + "没有找到成本对象,");
                                item.setStatus(CopyStatusEnums.CANT.getCode());
                            }
                        }

                        getAuxiliary(item);

                        item.setLastupdatetime(date);
                        item.setLastCopyTime(date);

                        this.copytokditemService.updateCopytokditem(item);
                    }
                }
                if (flag/* && StringUtils.isEmpty(c.getCopyRemark())*/) {
                    if (!c.getStatus().equals(CopyStatusEnums.SPLIT.getCode())) {
                        c.setStatus(CopyStatusEnums.WAIT.getCode());
                    }
                } else {
                    if (!c.getStatus().equals(CopyStatusEnums.SPLIT.getCode())) {
                        c.setStatus(CopyStatusEnums.CANT.getCode());
                    }

                }
                //2022-05-27 性能问题，改善checksale方法逻辑
                if (!flag && c.getParentid() != null) {
                    //c.getParentid 放在hashset中
                    if (parentIds.get() != null) {
                        parentIds.get().add(c.getParentid());
                    } else {
                        Set<Long> set = new HashSet<>();
                        set.add(c.getParentid());
                        parentIds.set(set);
                    }

                }

                c.setLastCopyTime(date);

                if (c.getCode().indexOf("SF") > -1) {
                    //销售单
                    MultipartConfig multipartConfig = multipartConfigService.selectMultipartConfigBySellPayType(c.getSellpaytype());
                    if (multipartConfig != null) {
                        c.setKdsellpaytype(multipartConfig.getKdsellpaytype());
                    }

                }

                this.copytokdService.updateCopytokd(c);

            }


            checkSale();


            SysConfig sysConfig1 = this.sysConfigService.selectConfigById(4L);
            sysConfig1.setConfigValue("4");
            this.sysConfigService.updateConfig(sysConfig1);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(e.getMessage());
        }

        parentIds.remove();
    }


    /**
     * 辅助数量
     *
     * @param copytokditem
     */
    public void getAuxiliary(Copytokditem copytokditem) {
        if (copytokditem.getLoadcount() != null) {
            if (copytokditem.getLoadcount().compareTo(BigDecimal.ZERO) != 0) {
                copytokditem.setAuxiliary(copytokditem.getLoadcount());
            } else {
                copytokditem.setAuxiliary(copytokditem.getQty());
            }
        } else {
            copytokditem.setAuxiliary(copytokditem.getQty());
        }
    }

    /**
     * 获取金蝶数量
     *
     * @param copytokditem
     * @param type
     */
    public void getKdQty(Copytokditem copytokditem, String type, Salesspecification salesspecification) {
        if (copytokditem.getKdqty() == null) {
            if (salesspecification.getKingdeeunitid() != null) {
                List<Stockunit> stockunits = stockunitList.stream().filter(s -> s.getId().equals(salesspecification.getKingdeeunitid())).collect(Collectors.toList());
                if (stockunits.size() == 1) {
                    if (stockunits.get(0).getIsUnit() == IsWeifhtUnitEnums.YES.getCode()) {
                        //是重量单位
                        List<Stockunit> stockunitsweight = this.stockunitList.stream().filter(tw -> tw.getId().equals(copytokditem.getUnitid())).collect(Collectors.toList());
                        if (stockunitsweight.size() == 1) {
                            copytokditem.setKdqty(copytokditem.getQty().multiply(stockunitsweight.get(0).getToT()).divide(stockunits.get(0).getToT()));
                        } else {
                            copytokditem.setCopyRemark(copytokditem.getCopyRemark() + "物料没有库存单位,");
                            copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                        }
                    } else {
                        copytokditem.setKdqty(copytokditem.getQty());
                        Stockunit unit = stockunitService.selectStockunitById(salesspecification.getStockunitid());
                        copytokditem.setKingdeeunit(unit.getKdunit());
                    }

                } else {
                    copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                    copytokditem.setCopyRemark("没有找到单位");
                }
            } else {
                copytokditem.setStatus(CopyStatusEnums.CANT.getCode());
                copytokditem.setCopyRemark("物料没有金蝶单位编码");
            }
        }
    }


    public void checkSale() {
        //2022-05-27 遍历获取hashset，得到数据加工没有成功的拆分订单，
        // 将所有的兄弟拆分订单（parentId相等的）都设置为不可推送
        if (parentIds.get() != null) {
            for (Long parentId : parentIds.get()) {
                Copytokd copy = new Copytokd();
                copy.setParentid(parentId);
                List<Copytokd> copytokds = this.copytokdService.selectCopytokdList(copy);
                for (Copytokd co : copytokds) {
                    if (!co.getStatus().equals(CopyStatusEnums.CANT.getCode())) {
                        co.setStatus(CopyStatusEnums.NO.getCode());
                        this.copytokdService.updateCopytokd(co);
                    }
                }
            }
        }


//        Copytokd copytokd = new Copytokd();
//        copytokd.setStatus(CopyStatusEnums.SPLIT.getCode());
//        List<Copytokd> copytokdList = this.copytokdService.selectCopytokdList(copytokd);
//        for(Copytokd c : copytokdList){
//
//            boolean spiltFlag = true;
//            Copytokd copy = new Copytokd();
//            copy.setParentid(c.getId());
//            List<Copytokd> copytokds = this.copytokdService.selectCopytokdList(copy);
//            for(Copytokd co : copytokds){
//                if(co.getStatus().equals(CopyStatusEnums.CANT.getCode())){
//                    spiltFlag = false;
//                }
//            }
//            if(!spiltFlag){
//                for(Copytokd co : copytokds){
//                    if(!co.getStatus().equals(CopyStatusEnums.CANT.getCode())) {
//                        co.setStatus(CopyStatusEnums.NO.getCode());
//                        this.copytokdService.updateCopytokd(co);
//                    }
//
//                }
//            }
//
//
//        }

    }


    /**
     * 获取内部价格
     *
     * @param copytokd
     * @param salesspecification
     * @return
     */
    public BigDecimal getInnprice(Copytokd copytokd, Salesspecification salesspecification) {
        if (!copytokd.getKdtype().equals(KDWhoutformTypeEnums.SALE.getCode()) &&
                !copytokd.getKdtype().equals(KDWhinformTypeEnums.PURCHASING.getCode())) {
            return BigDecimal.ZERO;
        }

        if (copytokd.getCode().substring(0, 2).equals("SF")) {
            Sellform sellform = this.sellformService.selectSellformByCode(copytokd.getCode());
            //入库单
            if (copytokd.getType() == BillTypeEnums.IN.getCode() && copytokd.getSubtype() == WhinformTypeEnums.PURCHASING.getCode()) {
                if (copytokd.getSupplierid() != null) {
                    List<Supplier> suppliers = supplierList.stream().filter(s -> 3 == s.getType() && copytokd.getSupplierid().equals(s.getId())).collect(Collectors.toList());
                    if (suppliers.size() == 1) {
                        List<Innerprice> innerprices = innerpriceList.stream().filter(i -> (suppliers.get(0).getOrgguid().equals(i.getOrgguid())) && (copytokd.getOrgguid().equals(i.getPurorgguid())) && (salesspecification.getGuid().equals(i.getSpguid()) && (i.getEffectivetime().getTime() < copytokd.getOrderdate().getTime()))).collect(Collectors.toList());
                        if (innerprices.size() > 0) {
                            if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)) {
                                return innerprices.get(0).getPrice();
                            } else {
                                if (sellform.getIsdiscount() == 1) {
                                    if (innDiscount == null) {
                                        return new BigDecimal(-1);
                                    } else {
                                        return innerprices.get(0).getPrice().multiply(BigDecimal.ONE.subtract(innDiscount));
                                    }
                                } else {
                                    return innerprices.get(0).getPrice();
                                }
                            }
                        } else {
                            //没有找到价格
                            //找只有供应商的数据
                            List<Innerprice> innerpricesu = innerpriceList.stream().filter(i -> (suppliers.get(0).getOrgguid().equals(i.getOrgguid())) && (salesspecification.getGuid().equals(i.getSpguid()) && (i.getEffectivetime().getTime() < copytokd.getOrderdate().getTime()))).collect(Collectors.toList());
                            if (innerpricesu.size() == 1) {
                                if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)) {
                                    return innerpricesu.get(0).getPrice();
                                } else {
                                    if (sellform.getIsdiscount() == 1) {
                                        if (innDiscount == null) {
                                            return new BigDecimal(-1);
                                        } else {
                                            return innerpricesu.get(0).getPrice().multiply(BigDecimal.ONE.subtract(innDiscount));
                                        }
                                    } else {
                                        return innerpricesu.get(0).getPrice();
                                    }
                                }
                            } else {
                                return new BigDecimal(-1);
                            }
                        }
                    } else {
                        return new BigDecimal(-1);
                    }
                } else {
                    return new BigDecimal(-1);
                }
            }
            //出库单
            if (copytokd.getType() == BillTypeEnums.OUT.getCode() && copytokd.getSubtype() == WhoutformTypeEnums.SALE.getCode()) {
                if (StringUtils.isNotEmpty(copytokd.getCustomerid())) {
                    List<Customer> customers = customerList.stream().filter(c -> 2 == c.getType() && copytokd.getCustomerid().equals(c.getGuid())).collect(Collectors.toList());
                    if (customers.size() == 1) {
                        List<Innerprice> innerprices = innerpriceList.stream().filter(i -> (copytokd.getOrgguid().equals(i.getOrgguid())) && (customers.get(0).getOrgguid().equals(i.getPurorgguid())) && (salesspecification.getGuid().equals(i.getSpguid()) && (i.getEffectivetime().getTime() < copytokd.getOrderdate().getTime()))).collect(Collectors.toList());
                        if (innerprices.size() > 0) {
                            if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)) {
                                return innerprices.get(0).getPrice();
                            } else {
                                if (sellform.getIsdiscount() == 1) {
                                    if (innDiscount == null) {
                                        return new BigDecimal(-1);
                                    } else {
                                        return innerprices.get(0).getPrice().multiply(BigDecimal.ONE.subtract(innDiscount));
                                    }
                                } else {
                                    return innerprices.get(0).getPrice();
                                }
                            }
                        } else {
                            //没有找到价格
                            //找只有供应商的数据
                            List<Innerprice> innerpricesu = innerpriceList.stream().filter(i -> (copytokd.getOrgguid().equals(i.getOrgguid())) && salesspecification.getGuid().equals(i.getSpguid())).collect(Collectors.toList());
                            if (innerpricesu.size() == 1) {
                                if (sellform.getPaytype().equals(4) || sellform.getPaytype().equals(5) || sellform.getPaytype().equals(6)) {
                                    return innerpricesu.get(0).getPrice();
                                } else {
                                    if (sellform.getIsdiscount() == 1) {
                                        if (innDiscount == null) {
                                            return new BigDecimal(-1);
                                        } else {
                                            return innerpricesu.get(0).getPrice().multiply(BigDecimal.ONE.subtract(innDiscount));
                                        }
                                    } else {
                                        return innerpricesu.get(0).getPrice();
                                    }
                                }
                            } else {
                                return new BigDecimal(-1);
                            }
                        }
                    } else {
                        return new BigDecimal(-1);
                    }
                } else {
                    return new BigDecimal(-1);
                }
            }
        }

        return new BigDecimal(-1);
    }


    /**
     * 是否拆分数据
     *
     * @param status
     * @return
     */
    public boolean getSplit(Integer status) {
        if (status == CopyStatusEnums.SPLIT.getCode()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取组织的销售中转库
     *
     * @param orgnazationsId
     * @return
     */
    public Storeroom getStoreRoomIdByOrgId(String orgnazationsId) {
        List<Storeroom> storerooms = storeroomList.stream().filter(s -> ("4".equals(s.getNature()) && orgnazationsId.equals(s.getOrgguid()))).collect(Collectors.toList());
        if (storerooms.size() == 1) {
            return storerooms.get(0);
        }
        return null;
    }

    public Storehouseroom getStoreHouseIdBySrId(String storeRoomId, int shType) {
        if (storeRoomId == null) {
            return null;
        }
        List<Storehouseroom> storehouserooms = storehouseroomList.stream().filter(s -> (storeRoomId.equals(s.getStoreguid()))).collect(Collectors.toList());
        storehouserooms = storehouserooms.stream().filter(s -> shType == s.getShrtype()).collect(Collectors.toList());
        if (storehouserooms.size() > 0) {
            return storehouserooms.get(0);
        }
        ;
        return null;
    }

    public Salesspecification getSpe(String spguid) {
        List<Salesspecification> salesspecifications = salesspecificationList.stream().filter(s ->
                spguid.equals(s.getGuid())).collect(Collectors.toList());
        if (salesspecifications.size() == 1) {
            return salesspecifications.get(0);
        }
        return null;
    }

}
